<!DOCTYPE html>
<html>

<head>
	<meta charset="UTF-8">
	<title>getElementsByTagName获取checkbox进行全选</title>
	<style type="text/css">
		#myTable {
			width: 900px;
			margin: 0px auto;
			background-color: #008000;
		}

		#myTable td,
		#myTable th {
			background-color: white;
		}

		#myDiv {
			width: 900px;
			margin: 0px auto;
			height: 30px;
			line-height: 30px;
			text-align: center;
		}
	</style>
	<!-- <script>======代码======</script> -->

</head>

<body>
	<table cellspacing="1" id="myTable">
		<tr>
			<th width="200">
				<input type="checkbox" id="selAll" onchange="choseAll()" />全选
				<input type="checkbox" id="selAll" onchange="esohc()" />反选
			</th>
			<th width="200">姓名</th>
			<th width="200">性别</th>
			<th width="200">专业</th>
			<th width="200">爱好</th>
		</tr>
		<tr>
			<td><input type="checkbox" /></td>
			<td>刘备</td>
			<td>男</td>
			<td>软件开发</td>
			<td>抽烟</td>
		</tr>
		<tr>
			<td><input type="checkbox" /></td>
			<td>关羽</td>
			<td>男</td>
			<td>国际贸易</td>
			<td>喝酒</td>
		</tr>
		<tr>
			<td><input type="checkbox" /></td>
			<td>张飞</td>
			<td>男</td>
			<td>园林设计</td>
			<td>烫头发</td>
		</tr>
		<tr>
			<td><input type="checkbox" /></td>
			<td>赵云</td>
			<td>男</td>
			<td>平面设计</td>
			<td>抽烟</td>
		</tr>
		<tr>
			<td><input type="checkbox" /></td>
			<td>黄忠</td>
			<td>男</td>
			<td>影视制作</td>
			<td>玩游戏</td>
		</tr>
		<tr>
			<td><input type="checkbox" /></td>
			<td>小乔</td>
			<td>女</td>
			<td>高级护理</td>
			<td>唱歌</td>
		</tr>
	</table>
	<script type="text/javascript">
		var input = document.querySelectorAll('input');

		for (let i = 0; i < input.length; i++) {
			// if (i>=2) {
			// 	input[i].setAttribute('onclick','click()');
			// }
			input[i].setAttribute('checked', 'checked');
			input[i].checked = false;
		}

		// function click() {
			// for (let i = 2; i < input.length; i++) {
			// 	let x = 0;
			// 	if (input[i].checked) {
			// 		x++;
			// 	} else {
			// 		input[0].checked = false;
			// 	}
			// }
			// if (x == input.length - 2) {
			// 	input[0].checked = true;
			// }
		// }

		function choseAll() {
			for (let i = 2; i < input.length; i++) {
				if (input[0].checked) {
					input[i].checked = true;
				} else {
					input[i].checked = false;
				}
			}
			console.log(input[2]);
		}

		function esohc() {
			for (let i = 2; i < input.length; i++) {
				if (input[i].checked) {
					input[i].checked = false;
				} else {
					input[i].checked = true;
				}
			}
		}
	</script>
</body>

</html>